'use strict';

const Controller = require('egg').Controller;

class SearchController extends Controller {
    /**
     * 打开搜索页面
     */
    async showSearchPage(){
        const { ctx } = this;
        ctx.logger.info('>> showSearchPage()');
        const serviceId = ctx.session.serviceId;
        let sortList = {};
        ctx.session.sortPoint = null; // 清空缓存搜索内容
        ctx.state.httpOption = {
            url: ['public', 'searchList'],
            data: {
                serviceId
            }
        }
        await this.service.handleRequest.requestDate()
        ctx.logger.info('>> has get searchList data');
        if (ctx.status === 200) 
            sortList = ctx.state.result.data;

        ctx.logger.info('>> render search page');
        await this.ctx.render('search',{
            title:'公共服务',
            sortList
        })
    }
    
    /**
     * 保存搜索数据
     */
    async saveSearchLocation(){
        const {ctx} = this;
        ctx.logger.info('>> saveSearchLocation()');
        ctx.logger.info(ctx.request.body);
        if (ctx.request.body) {
            ctx.session.sortPoint = ctx.request.body;
            ctx.logger.info('>> save sortPoint in session');
            ctx.body = {
                status: 200,
                message: '保存成功'
            }
        }
        else {
            ctx.logger.info('>> save sortPoint is undefined');
            ctx.session.sortPoint = null;
            ctx.body = {
                status: 403,
                message: '未找到保存数据'
            }
        }      
    }

}

module.exports = SearchController;